#include "GLXTest.h"

int main(int argc, char* argv[])
{
    GLEngine* engine = new GLEngine("GLXTest - F1 to load model", 400, 400, 32, false, 0, 0);
    engine->createClient<GLXTest>("GLXTest");
    engine->runClient("GLXTest");
    delete engine;
}


//
// GLXObject
//

GLXObject::GLXObject(SceneGraph* scene) :
    SceneObject(scene)
{
    vertex_data_size = 0;
    index_data_size = 0;
    vertices = NULL;
    texcoords = NULL;
    normals = NULL;
    indices = NULL;
}

GLXObject::~GLXObject()
{
}

void GLXObject::onUpdate(float dt)
{
}

void GLXObject::onRender()
{
    if (indices)
    {
        glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
        if (vertices)
        {
            glEnableClientState(GL_VERTEX_ARRAY);
            glVertexPointer(3, GL_FLOAT, 0, vertices);
        }
        glDrawElements(GL_TRIANGLES, index_data_size, GL_UNSIGNED_SHORT, indices);
        glPopClientAttrib();
    }
}

void GLXObject::loadObj(const char* filename)
{
    UTModel* m = new UTModel(filename);
    if (m)
    {
        printf("load model %i %i\n", m->verticesCount, m->groupsCount);

        vertex_data_size = m->verticesCount;
        for (int i = 0; i < m->groupsCount; i++)
        {
            index_data_size += m->groups[i].facesCount * 3;
        }

        vertices = new vector3f [vertex_data_size];
        indices = new int [index_data_size];

        memcpy(vertices, m->vertices, vertex_data_size * sizeof(vector3f));

        int ii = 0;
        for (int g = 0; g < m->groupsCount; g++)
        {
            for (int i = 0; i < m->groups[g].facesCount; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    indices[ii] = m->groups[g].faces[i].vind[j];
                    ii++;
                }
            }
        }

        delete m;
    }
}


//
// GLXTest
//

GLXTest::GLXTest(GLEngine* engine) :
    GLClient(engine)
{
    camera.zoom = -30;
    //camera.position.set(50, 50, 0);
    camera.rotation.set(-70, 0, 30);

    scene = new SceneGraph(engine);

    SceneBox* floor = scene->createObject<SceneBox>();
    floor->setScale(30, 30, 0.1);
    floor->setPosition(0, 0, -0.1);

    GLXObject* glx = scene->createObject<GLXObject>();
    glx->loadObj("gfx/seats.obj");

    SceneModel* obj = scene->createObject<SceneModel>();
    obj->setModel(this->getModel("gfx/seats.obj"));
    obj->setPosition(10, 0, 0);
}

GLXTest::~GLXTest()
{
}

void GLXTest::onEnter()
{
}

void GLXTest::onLeave()
{
}

void GLXTest::onEvent(GLEvent e)
{
    switch (e.type)
    {
    case EVT_KeyPress:
        {
            if (e.key.code == KEY_ESC)
            {
                getEngine()->quit(0);
            }
            else if (e.key.code == KEY_F1)
            {
                char fn[1024];
                if (show_open_box(fn, "AVI files|*.avi"))
                {
                }
            }
        }
        break;
    case EVT_MouseMove:
        {
            int dx = e.mouse.x - mouse.x;
            int dy = e.mouse.y - mouse.y;
            if (mouse.button[MOUSE_RIGHT])
            {
                camera.rotation.z += dx;
                camera.rotation.x += dy;
            }
            if (mouse.button[MOUSE_LEFT])
            {
                camera.translate2D(-dx*0.1, dy*0.1);
            }
        }
        break;
    case EVT_MouseWheel:
        {
            camera.zoom -= e.mouse.wheel;
        }
        break;
    }
}

void GLXTest::onUpdate(float dt)
{
    scene->update(dt);
}

void GLXTest::onDraw()
{
    glBeginScene3D();
    camera.apply();
    scene->render();
    glEndScene3D();
}





